package com.spelet.simulation;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.utils.ScissorStack;
import com.spelet.StaticFonts;
import com.spelet.StaticSprites;
import com.spelet.StaticTextures;
import com.spelet.StaticVariables;
import com.spelet.VariableCollection;

public class ChooseBlockBrush {
	public int[] valToVarVal;
	public Sprite background;
	public Sprite border;
	public Sprite yes;
	public Sprite no;
	public float deltaX;
	public Vector2[] positions;
	public Sprite marked;
	public Rectangle rb;
	public int selectedType;
	public String[] descriptions;
	int clickedAt;
	public ChooseBlockBrush() {
		positions = new Vector2[StaticVariables.amountOfBlockTypes];
		descriptions = new String[StaticVariables.amountOfBlockTypes];
		valToVarVal = new int[StaticVariables.amountOfBlockTypes];
		valToVarVal[StaticVariables.boulderVal] = VariableCollection.boulder;
		valToVarVal[StaticVariables.moveVal] = VariableCollection.moveBlock;
		valToVarVal[StaticVariables.wallVal] = VariableCollection.wall;
		valToVarVal[StaticVariables.reddoorVal] = VariableCollection.door_red;
		valToVarVal[StaticVariables.redkeyVal] = VariableCollection.key_red;
		valToVarVal[StaticVariables.eraserVal] = VariableCollection.EditorEraser;
		valToVarVal[StaticVariables.goalVal] = VariableCollection.goal;
		valToVarVal[StaticVariables.bluedoorVal] = VariableCollection.door_blue;
		valToVarVal[StaticVariables.bluekeyVal] = VariableCollection.key_blue;
		valToVarVal[StaticVariables.yellowdoorVal] = VariableCollection.door_yellow;
		valToVarVal[StaticVariables.yellowkeyVal] = VariableCollection.key_yellow;
		valToVarVal[StaticVariables.mergeVal] = VariableCollection.mergeblock;
		//Background
		background = StaticTextures.mapEdit.createSprite("background");
		border = StaticTextures.mapEdit.createSprite("border");
		marked = StaticTextures.mapEdit.createSprite("marked");
		yes = StaticTextures.mapEdit.createSprite("yes");
		no = StaticTextures.mapEdit.createSprite("no");
		marked.setPosition(180, 350);
	
		//boulder
		positions[StaticVariables.boulderVal] = new Vector2(100,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.boulderVal] = "A boulder. Will roll until it hits a wall if pushed.";
		//moveblock
		positions[StaticVariables.moveVal] = new Vector2(180,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.moveVal] = "A move block. Can be pushed in any direction. Will fill holes if pushed into them.";
		//wall
		positions[StaticVariables.wallVal] = new Vector2(260,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.wallVal] = "An indestructable wall. You will not get past this. Seriously, you will not.";
		//Redkey
		positions[StaticVariables.redkeyVal] = new Vector2(340,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.redkeyVal] = "A red key. Can be used to unlock a red door.";
		//Reddoor
		positions[StaticVariables.reddoorVal] = new Vector2(420,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.reddoorVal] = "A red door. Can be unlocked using a red key.";
		//Eraser
		positions[StaticVariables.eraserVal] = new Vector2(500,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.eraserVal] = "Eraser..Use wisely to remove blocks!...Wait.. did someone say Erasure?";
		//Goal
		positions[StaticVariables.goalVal] = new Vector2(580,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.goalVal] = "THIS, THIS IS THE GOAL.";
		//Blue key
		positions[StaticVariables.bluekeyVal] = new Vector2(660,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.bluekeyVal] = "A blue key. Can be used to unlock a blue door.";
		//Blue door
		positions[StaticVariables.bluedoorVal] = new Vector2(740,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.bluedoorVal] = "A blue door. Can be unlocked using a blue key.";
		//Yellow door
		positions[StaticVariables.yellowkeyVal] = new Vector2(820,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.yellowkeyVal] = "A yellow key. Can be used to unlock a yellow door.";
		//Yellow door
		positions[StaticVariables.yellowdoorVal] = new Vector2(900,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.yellowdoorVal] = "A yellow door. Can be unlocked using a yellow key.";
		//Mergeblock
		positions[StaticVariables.mergeVal] = new Vector2(980,Gdx.graphics.getHeight()/2+110);
		descriptions[StaticVariables.mergeVal] = "Mergeblock. Push it into another mergeblock to make them both dissapear! Like a rabbit in a wizard hat! Or a cat..";
		
		background.setX(Gdx.graphics.getWidth()*0.88f/2-background.getWidth()/2);
		background.setY(Gdx.graphics.getHeight()/2-background.getHeight()/2*0.47f);
		border.setX(Gdx.graphics.getWidth()*0.88f/2-border.getWidth()/2);
		border.setY(Gdx.graphics.getHeight()/2-border.getHeight()/2*0.47f);
		rb = background.getBoundingRectangle();
		for (int i = 0; i < StaticVariables.amountOfBlockTypes; i++) {
			String[] strings = descriptions[i].split(" ");
			String finalString = "";
			String str=strings[0];
			for (int j = 1; j < strings.length; j++) {
				String s = strings[j];
				if (StaticFonts.basicFont.getBounds(str+" "+s).width > 300) {
					finalString += str+"\n";
					str = s;
				} else {
					str =str+ " " + s;
				}
			}
			finalString += str;
			descriptions[i] = finalString;
		}
	}
	public void update() {
		yes.setPosition(background.getX()+background.getWidth()-yes.getWidth()-5,background.getY()+no.getHeight()+6);
		no.setPosition(background.getX()+background.getWidth()-no.getWidth()-5,background.getY()+5);
		if (deltaX < -280)
			deltaX = -280;
		else if (deltaX > (StaticVariables.amountOfBlockTypes-1)*80-200)
			deltaX=(StaticVariables.amountOfBlockTypes-1)*80-200;
	}
	public void render(OrthographicCamera cam, SpriteBatch sb) {
		background.draw(sb);
		ScissorStack.calculateScissors(cam, sb.getTransformMatrix(), rb, rb);
		ScissorStack.pushScissors(rb);
		for (int i = 0; i < StaticVariables.amountOfBlockTypes; i++) {
			Sprite s = StaticSprites.cbbSprites[i];
			s.setPosition(positions[i].x-deltaX, positions[i].y);
			s.draw(sb);
			if (selectedType == i) {
				marked.setPosition(positions[i].x-deltaX,positions[i].y);
				marked.draw(sb);
			}
		}
//		marked.draw(sb);
		border.draw(sb);
		yes.draw(sb);
		no.draw(sb);
		sb.flush();
		ScissorStack.popScissors();
		StaticFonts.basicFont.drawMultiLine(sb, descriptions[selectedType], background.getX()+10, background.getY()+background.getHeight()*0.5f);
	}
	public boolean hit(int x, int y) {
		return (rb.contains(x,y));
	}
	public void pressed(float x, float y) {
		for (int i = 0; i < StaticVariables.amountOfBlockTypes;i++) {
			if (StaticSprites.cbbSprites[i].getBoundingRectangle().contains(x,y)) {
				StaticSprites.cbbSprites[i].setPosition(positions[i].x+deltaX,positions[i].y);
				selectedType = i;
			}
		}
	}
	public void changeDelta(int delta) {
		deltaX += delta;
	}
}
